# coding:utf-8
"""
常规测试案例管理
"""

import appconf
import datetime, uuid
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy(appconf.app)


class CaseResult(db.Model):
    """测试结果的存储"""
    __tablename__ = 'case_result'
    id = db.Column(db.Integer, primary_key=True)
    case_id = db.Column(db.String(10), nullable=True)
    project_id = db.Column(db.Integer, nullable=True)
    case_version = db.Column(db.String(10), nullable=True)
    project_version = db.Column(db.String(500), nullable=True)
    result = db.Column(db.String(10), nullable=True)
    bug_url = db.Column(db.String(500), nullable=True)
    bug_des = db.Column(db.String(500), nullable=True)
    person = db.Column(db.String(50), nullable=True)
    last_time = db.Column(db.String(50), nullable=True)

    def __init__(self, case_id,project_id,case_version,project_version, result, bug_url, bug_des, person):
        self.case_version = case_version
        self.project_id = project_id
        self.project_version = project_version
        self.result = result
        self.bug_url = bug_url
        self.bug_des = bug_des
        self.person = person
        self.case_id = case_id
        self.last_time = str(datetime.datetime.now())

    def add(self):
        db.session.add(self)
        db.session.commit()
        return self.id


class HisModify(db.Model):
    __tablename__ = 'His_Modify'
    id = db.Column(db.Integer, primary_key=True)
    case_id = db.Column(db.Integer, nullable=True)
    # 0为用例,1为执行结果
    modify_cty = db.Column(db.Integer, nullable=True)
    modify_ctn = db.Column(db.Text, nullable=True)

    # 0为创建,1为更新,3为删除
    modify_order = db.Column(db.Integer, nullable=True)
    up_person = db.Column(db.String(50), nullable=True)
    last_time = db.Column(db.String(50), nullable=True)

    def __init__(self, case_id, modify_cty, modify_ctn, modify_order, up_person):
        self.case_id = case_id
        self.modify_cty = modify_cty
        self.modify_ctn = modify_ctn
        self.up_person = up_person
        self.modify_order = modify_order
        self.last_time = str(datetime.datetime.now())

    def add(self):
        db.session.add(self)
        db.session.commit()


class CasePlanProcess(db.Model):
    """测试进度汇报"""
    __tablename__ = "case_plan_process"
    id = db.Column(db.Integer, primary_key=True)
    case_plan_id = db.Column(db.Integer, nullable=True)
    case_sub_plan_id = db.Column(db.Integer, nullable=True)
    process = db.Column(db.Integer, nullable=True)
    note = db.Column(db.String(500), nullable=True)
    up_person = db.Column(db.String(50), nullable=True)
    update_time = db.Column(db.String(50), nullable=True)

    def __init__(self, case_plan_id, case_sub_plan_id, process, note, up_person):
        self.case_plan_id = case_plan_id
        self.case_sub_plan_id = case_sub_plan_id
        self.process = process
        self.note = note
        self.up_person = up_person
        self.update_time = str(datetime.datetime.now())

    def add(self):
        db.session.add(self)
        db.session.commit()
        return self.id


class CaseSubPlan(db.Model):
    __tablename__ = "case_sub_plan"

    id = db.Column(db.Integer, primary_key=True)
    case_plan_id = db.Column(db.Integer, nullable=True)
    #   工作类型
    work_type = db.Column(db.Integer, nullable=True)
    #   工作内容描述
    work_content = db.Column(db.Text, nullable=True)

    #   计划开始、结束时间、实际时间
    p_end = db.Column(db.String(50), nullable=True)
    c_end = db.Column(db.String(50), nullable=True)

    person = db.Column(db.String(50), nullable=True)
    update_time = db.Column(db.String(50), nullable=True)

    def __init__(self, case_plan_id, work_type, work_content, p_end, c_end, person):
        self.case_plan_id = case_plan_id
        self.work_type = work_type
        self.work_content = work_content
        self.c_end = c_end
        self.p_end = p_end
        self.person = person
        self.update_time = str(datetime.datetime.now())

    def add(self):
        db.session.add(self)
        db.session.commit()
        return self.id


class CasePlan(db.Model):
    """主任务安排"""
    __tablename__ = "case_plan"
    id = db.Column(db.Integer, primary_key=True)
    #   项目Id
    project_id = db.Column(db.Integer, nullable=True)
    #   版本号
    project_version = db.Column(db.String(500), nullable=True)
    #   测试阶段
    plan_type = db.Column(db.Integer, nullable=True)

    #   计划开始、结束时间、实际时间
    work_p_start = db.Column(db.String(50), nullable=True)
    work_p_end = db.Column(db.String(50), nullable=True)
    work_end = db.Column(db.String(50), nullable=True)
    #   创建计划0,进行中,1，关闭2，暂停3,废弃4
    work_state = db.Column(db.String(50), nullable=True)
    #   负责人
    work_charge = db.Column(db.String(50), nullable=True)
    #   谁创建的谁负责修改
    up_person = db.Column(db.String(50), nullable=True)
    update_time = db.Column(db.String(50), nullable=True)

    def __init__(self, project_id, project_version, plan_type, work_p_start, work_p_end,
                 work_end, work_state, work_charge, up_person):
        self.project_id = project_id
        self.project_version = project_version
        self.plan_type = plan_type
        self.work_p_start = work_p_start
        self.work_p_end = work_p_end
        self.work_end = work_end
        self.work_state = work_state
        self.work_charge = work_charge
        self.up_person = up_person
        self.update_time = str(datetime.datetime.now())

    def add(self):
        db.session.add(self)
        db.session.commit()
        return self.id


class BaseCase(db.Model):
    __tablename__ = 'Base_Case'
    id = db.Column(db.Integer, primary_key=True)
    project_id = db.Column(db.Integer, nullable=True)

    case_type = db.Column(db.Integer, nullable=True)

    case_plan = db.Column(db.String(50), nullable=True)
    # 需求编号
    case_require_no = db.Column(db.String(50), nullable=True)

    case_no = db.Column(db.String(500), nullable=True)
    case_main_mod = db.Column(db.String(100), nullable=True)
    case_second_mod = db.Column(db.String(100), nullable=True)
    case_pri = db.Column(db.String(10), nullable=True)

    # 前提条件
    case_pre = db.Column(db.Text, nullable=True)
    case_title = db.Column(db.Text, nullable=True)
    case_step = db.Column(db.Text, nullable=True)
    case_expect = db.Column(db.Text, nullable=True)
    case_note = db.Column(db.Text, nullable=True)

    is_show = db.Column(db.Integer, nullable=True)

    in_person = db.Column(db.String(50), nullable=True)
    up_person = db.Column(db.String(50), nullable=True)

    last_time = db.Column(db.String(50), nullable=True)
    update_time = db.Column(db.String(50), nullable=True)
    imag_desc = db.Column(db.Text, nullable=True)
    autofun = db.Column(db.String(500), nullable=True)

    case_fun = db.Column(db.Integer, nullable=True)

    def __init__(self, project_id, case_type, case_plan, case_title,
                 case_require_no='', case_main_mod='',
                 case_pri='高', case_pre="", case_step='', case_second_mod='',
                 case_expect='', case_note='', in_person='', up_person='', imag_desc="", autofun="", case_fun=""):
        strUid = "%s" % uuid.uuid1()
        self.case_fun = case_fun
        self.project_id = project_id
        self.case_plan = case_plan
        self.case_type = case_type
        self.case_require_no = case_require_no
        self.case_pre = case_pre
        self.case_title = case_title
        self.case_main_mod = case_main_mod
        self.case_pri = case_pri
        self.case_step = case_step
        self.case_second_mod = case_second_mod
        self.case_expect = case_expect
        self.case_note = case_note
        self.case_no = "%s_%s" % (self.case_plan, strUid[0:18])
        self.is_show = 0
        self.in_person = in_person
        self.up_person = up_person
        self.imag_desc = imag_desc
        self.autofun = autofun

        self.update_time = str(datetime.datetime.now())
        self.last_time = str(datetime.datetime.now())

    def add(self):
        db.session.add(self)
        db.session.commit()
        return self.id

    def row_to_dict(self):
        dict = {c.name: getattr(self, c.name, None) for c in self.__table__.columns}
        return dict


db.create_all()
